package com.hbisedm.interninfosys.intern.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.hbisedm.interninfosys.intern.entity.message.SampleMessage;
import com.hbisedm.interninfosys.intern.websocket.WebSocket;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/socket")
public class TestSocket {
    @Autowired
    WebSocket webSocket;

    /**
     * 发送给指定的用户信息
     */
    @GetMapping("/toChat")
    public R importExcel(@Param("shipId") String shipId) throws InterruptedException {
//        System.out.println("ship =>" + shipId);
        //        for (int i = 0; i < 10; i++){
        //            Thread.sleep(1000);
        //            webSocket.sendTextMessage(shipId, ""+i);
        //        }
//        webSocket.sendTextMessage(shipId, "ni好"+shipId);
        SampleMessage sampleMessage = new SampleMessage();
        sampleMessage.setType("say");
        sampleMessage.setName("Sam");
        sampleMessage.setMsg("hello");
        webSocket.sendTextMessage(shipId, JSON.toJSONString(sampleMessage));
        return R.ok("success");
    }



}
